package com.imarkket.Library;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.imarkket.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Sistemas on 23/05/2014.
 */

public class c_app_status {

    private static c_app_status instance = new c_app_status();
    static Context context;
    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;
    boolean connected = false;

    public static c_app_status getInstance(Context ctx) {
        context = ctx;
        return instance;
    }

    public boolean isOnline(Context con) {
        try {
            connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            connected = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
            return connected;

        } catch (Exception e) {
            c_logging.getInstance().log(c_logging.LOG_ERROR, "isOnline: " + e.toString());
        }
        return connected;
    }

    /**
     * Get all connection details 1. Status 2. Type: Mobile/wifi 3. Sub type 4. Roaming status (Only for mobile network)
     *
     * @return Map<String, String> http://www.compiletimeerror.com/2013/06/check- internet-connection-in-android.html#.Uibe2DaGG3N
     */
    private Map<String, String> getConnectionDetails(Context con) {
        Map<String, String> networkDetails = new HashMap<String, String>();
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (wifiNetwork != null && wifiNetwork.isConnected()) {
                networkDetails.put("Type", wifiNetwork.getTypeName());
                networkDetails.put("Sub type", wifiNetwork.getSubtypeName());
                networkDetails.put("State", wifiNetwork.getState().name());
            }

            NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mobileNetwork != null && mobileNetwork.isConnected()) {
                networkDetails.put("Type", mobileNetwork.getTypeName());
                networkDetails.put("Sub type", mobileNetwork.getSubtypeName());
                networkDetails.put("State", mobileNetwork.getState().name());
                if (mobileNetwork.isRoaming()) {
                    networkDetails.put("Roming", "YES");
                } else {
                    networkDetails.put("Roming", "NO");
                }
            }
        } catch (Exception e) {
            // networkDetails.put("Status", e.getMessage());
            c_logging.getInstance().log(c_logging.LOG_ERROR, "getConnectionDetails: " + e.toString());
        }
        return networkDetails;
    }

    // Determinamos el tipo de conexion
    public String getTypeStatus() {
        Map<String, String> networkDetails = getConnectionDetails(context);
        String salida = "networkDetails: ";

        if (networkDetails.isEmpty()) {
            salida += context.getString(R.string.st_la_Status_Empty);
        } else {
            if (networkDetails.containsKey("State")) {
                salida += "Status: " + networkDetails.get("State") + " ";
            }
            if (networkDetails.containsKey("Type")) {
                salida += "Type: " + networkDetails.get("Type") + " ";
            }
            if (networkDetails.containsKey("Sub type")) {
                salida += "SubType: " + networkDetails.get("Sub type") + " ";
            }
            if (networkDetails.containsKey("Roming")) {
                salida += "Roming: " + networkDetails.get("Roming") + " ";
            }
        }
        return salida;
    }
}